<?php

/**
 * AcceptProposedChange function 
 * 
 * Accept a ShiftChange 
 * 
 * @author Will <kexinzhao666@gmail.com> 
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
  /**
 * @param $userAuthName 
 * @param $userAuthPassHash 
 * @param $empID
 * @param $roleID 
 * @param $dateID
 * @param $myEmpID
 * @return array 
 */
function AcceptShiftChange($userAuthName, $userAuthPassHash, $empID, $roleID, $dateID, $myEmpID) {
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;
    if (checkAuthentication($userAuthName, $userAuthPassHash) == true) {
        try {
            $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
            mysqli_query($conn, "BEGIN");
            $sql1 = "delete from proposedchange where empID=" . $empID . " and roleID=" . $roleID . " AND dateID=" . $dateID;
            $sql2 = "update roledate set empID=" . $myEmpID . ", shiftStatus=\"Occupied\" where empID=" . $empID . " and roleID=" . $roleID . " AND dateID=" . $dateID;
            $rs1 = mysqli_query($conn, $sql1);
            $rs2 = mysqli_query($conn, $sql2);

            if ($rs1 && $rs2) {
                mysqli_query($conn, "COMMIT");
                $error = ErrorCode::successUpdate;
            } else {
                mysqli_query($conn, "ROLLBACK");
                $error = ErrorCode::failUpdate;
            }
        } catch (Excetpion $e) {
            mysqli_query($conn, "ROLLBACK");
            $error = ErrorCode::failUpdate;
            $msg = $e->getMessage();
            $conn->close();
            return array($error, $msg);
        }
    } else {
        $error = ErrorCode::authFailUpdate;
    }
    $conn->close();
    return array($error);
}

?>